package MallIntegralMall

import (
	CoreSQL "gitee.com/fotomxq/weeekj_core/v5/core/sql"
	CoreSQLPages "gitee.com/fotomxq/weeekj_core/v5/core/sql/pages"
	MallCore "gitee.com/fotomxq/weeekj_core/v5/mall/core"
	Router2SystemConfig "gitee.com/fotomxq/weeekj_core/v5/router2/system_config"
	"strings"
)

// ArgsGetIntegralProductList 获取积分商品列表参数
type ArgsGetIntegralProductList struct {
	//分页
	Pages CoreSQLPages.ArgsDataList `json:"pages"`
	//组织ID
	// -1 跳过
	OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
	//是否会员
	IsUserBuy bool `db:"is_user_buy" json:"IsUserBuy" check:"bool"`
	//是否删除
	IsRemove bool `db:"is_remove" json:"isRemove" check:"bool"`
	//搜索
	Search string `json:"search" check:"search" empty:"true"`
}

// GetIntegralProductList 获取积分商品列表
func GetIntegralProductList(args *ArgsGetIntegralProductList) (dataList []MallCore.FieldsCore, dataCount int64, err error) {
	where := ""
	maps := map[string]interface{}{}
	where = CoreSQL.GetDeleteSQL(args.IsRemove, where)
	if args.OrgID > -1 {
		where = where + " AND org_id = :org_id"
		maps["org_id"] = args.OrgID
	}
	if args.Search != "" {
		args.Search = strings.ReplaceAll(args.Search, " ", "")
		where = where + " AND (REPLACE(title, ' ', '') ILIKE '%' || :search || '%' OR REPLACE(title_des, ' ', '') ILIKE '%' || :search || '%' OR REPLACE(des, ' ', '') ILIKE '%' || :search || '%')"
		maps["search"] = args.Search
	}
	if args.Pages.Sort == "sort" {
		args.Pages.Sort = "sort, update_at"
	}
	where = where + " and is_integral_mall = true"
	where = where + " AND is_user_buy = :is_user_buy"
	maps["is_user_buy"] = args.IsUserBuy

	tableName := "mall_core"
	var rawList []MallCore.FieldsCore
	dataCount, err = CoreSQL.GetListPageAndCount(
		Router2SystemConfig.MainDB.DB,
		&rawList,
		tableName,
		"id",
		"SELECT id "+"FROM "+tableName+" WHERE "+where,
		where,
		maps,
		&args.Pages,
		[]string{"id", "create_at", "update_at", "delete_at", "sort", "sort, update_at", "expire_at", "audit_at", "price", "price_real", "integral", "buy_count",
			"is_integral_mall", "is_user_buy", "mall_integral_price", "integral_mall_count"},
	)
	if err != nil {
		return
	}
	for _, v := range rawList {
		vData := getIntegralMallByID(v.ID)
		if vData.ID < 1 {
			continue
		}
		dataList = append(dataList, vData)
	}
	return
}

// 获取商品信息
func getIntegralMallByID(id int64) (data MallCore.FieldsCore) {
	cacheMark := getIntegralMallCacheMark(id)
	if err := Router2SystemConfig.MainCache.GetStruct(cacheMark, &data); err == nil && data.ID > 0 {
		return
	}
	_ = Router2SystemConfig.MainDB.Get(&data, "SELECT id, create_at, update_at, delete_at, parent_id, org_id, product_type, is_virtual, sort, publish_at, sort_id, tags, code, title, title_des, des, cover_file_ids, des_files, currency, price_real, price_expire_at, price, price_no_tax, integral, integral_price, integral_transport_free, user_sub_price, user_ticket, transport_id, address, warehouse_product_id, weight, count, buy_count, other_options, giving_tickets, params, is_integral_mall, is_user_buy, mall_integral_price, integral_mall_count FROM mall_core WHERE id = $1", id)
	if data.ID < 1 {
		return
	}
	Router2SystemConfig.MainCache.SetStruct(cacheMark, data, 86400)
	return
}
